package com.cuvretool.view.inputlistener;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import com.cuvretool.InputListener;
import com.cuvretool.ModelInputHandleListener;
import com.cuvretool.View;
import com.cuvretool.model.detail.DrawString;
import com.cuvretool.model.detail.ModelGroup;
import com.cuvretool.utils.Vector2;
import com.cuvretool.view.coordinate.RulerDrawInfo;
import com.cuvretool.view.coordinate.workspace.TransformWorkSpace;
import com.cuvretool.view.draw.DisplayDraw;
import com.cuvretool.view.jframe.JFrameApplication;
import com.cuvretool.view.jpanel.JPanelOperate;
/**
 * View内外抛的处理事件（View内部自行解决）
 * since 2015年1月12日 下午11:36:29
 * @author boy
 */
public class ViewInputHandleListener implements InputListener {

	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================
	/**坐标转换方式定义*/
	public TransformWorkSpace mTransformWorkSpace;
	/**标尺绘制需求参数结构定义*/
	public RulerDrawInfo mRulerDrawInfo;
	/**坐标转换*/
	private DisplayDraw mDisplayDraw;
	/**操作面板*/
	private JPanelOperate mOperatePanel;
	/**窗口*/
	private JFrameApplication mFrameApplication;
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	/**
	 * 设置操作面板树结构参照骨架
	 */
	public void setOperateTree(ArrayList<ModelGroup> groups){
		mOperatePanel.setOperateTree(groups);
	}
	/**
	 * 设置操作面板
	 * @param panel
	 */
	public void setOperatePanel(JPanelOperate panel){
		mOperatePanel = panel;
	}
	/**
	 * 设置窗口
	 * @param frame
	 */
	public void setFrameApplication(JFrameApplication frame){
		mFrameApplication = frame;
	}
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void over() {
		// TODO Auto-generated method stub
		
	}
	// ===========================================================
	// Methods
	// ===========================================================
	/**
	 * 绘制区抛出的自定义绘制[屏幕坐标]
	 * @param g
	 */
	public void WorkSpaceDraw(Graphics g) {
		mDisplayDraw = DisplayDraw.obtain();
		
		ModelInputHandleListener modelInputHandleListener = View.getModelInputListener();
		modelInputHandleListener.setDisplayDraw(mDisplayDraw);
		modelInputHandleListener.DrawTheWorld(g);
		modelInputHandleListener.run();
		
		mDisplayDraw.TransformDrawToDisplay();
		
		g.setColor(Color.WHITE);
		ArrayList<Vector2> temp = mDisplayDraw.getSPoints();
		for (int i = 0; i < temp.size(); i++) {
			Vector2 pos = temp.get(i);
			g.drawLine((int)pos.x, (int)pos.y, (int)pos.x, (int)pos.y);
		}
		
		g.setColor(Color.BLUE);
		ArrayList<ArrayList<Vector2>> tempList = mDisplayDraw.getSLines();
		for (int i = 0; i < tempList.size(); i++) {
			temp = tempList.get(i);
			for (int j = 0; j < temp.size()-1; j++) {
				Vector2 pos1 = temp.get(j);
				Vector2 pos2 = temp.get(j+1);
				g.drawLine((int)pos1.x, (int)pos1.y, (int)pos2.x, (int)pos2.y);
			}
		}
		
		g.setColor(Color.RED);
		tempList = mDisplayDraw.getSPolygon();
		for (int i = 0; i < tempList.size(); i++) {
			temp = tempList.get(i);
			for (int j = 0; j < temp.size()-1; j++) {
				Vector2 pos1 = temp.get(j);
				Vector2 pos2 = temp.get(j+1);
				g.drawLine((int)pos1.x, (int)pos1.y, (int)pos2.x, (int)pos2.y);
			}
		}
		
		g.setColor(Color.YELLOW);
		ArrayList<DrawString> strings= mDisplayDraw.getSString();
		for (int i = 0; i < strings.size(); i++) {
			DrawString pos = strings.get(i);
			g.drawString(pos.mString, (int)pos.mPosition.x, (int)pos.mPosition.y);
		}
	}
	/**
	 * 点击到屏幕上一点
	 * @param pos		逻辑坐标点
	 * @param count		点击的次数
	 * @return 			true为需要刷新workspace，false则不需要刷新
	 */
	public boolean onClickEventWorkSpace(Vector2 pos, int count){
		mDisplayDraw = DisplayDraw.obtain();
		Vector2 result = mDisplayDraw.TransformDisplayToDraw(pos);
		ModelInputHandleListener modelInputHandleListener = View.getModelInputListener();
		boolean msg = false;
		if(count == 1){
			modelInputHandleListener.onClickOnly(result);
		} else if(count == 2){
			msg = modelInputHandleListener.onClickDouble(result);
		}
		return msg;
	}
	/**
	 * 按下鼠标左键或右键
	 * @param button
	 * @param pos
	 * @return
	 */
	public boolean onPressed(int button,Vector2 pos){
		Vector2 result = mDisplayDraw.TransformDisplayToDraw(pos);
		return View.getModelInputListener().onPressed(button,result);
	}
	/**
	 * 左键按下时，移动事件执行外抛
	 * @param pos	移动的位置坐标
	 * @return		true为需要更新曲线，false则无需更新
	 */
	public boolean onMovePoint(Vector2 pos){
		mDisplayDraw = DisplayDraw.obtain();
		Vector2 result = mDisplayDraw.TransformDisplayToDraw(pos);
		return View.getModelInputListener().onDragPoint(result);
	}
	/**
	 * 鼠标up事件，释放对应的状态
	 * @param button
	 */
	public void onReleased(int button){
		View.getModelInputListener().onReleased(button);
	}
	/**
	 * 鼠标在绘制区移动
	 * @param pos
	 * @return
	 */
	public boolean onMove(Vector2 pos){
		mDisplayDraw = DisplayDraw.obtain();
		Vector2 result = mDisplayDraw.TransformDisplayToDraw(pos);
		return View.getModelInputListener().onMove(result);
	}
	/**
	 * 键盘按下事件
	 * @param ｅ
	 * @return	true需要更新，false不需要更新
	 */
	public boolean onKeyPressed(KeyEvent e){
		return View.getModelInputListener().onKeyPressed(e);
	}
	/**
	 * 键盘释放事件
	 * @param ｅ
	 * @return	true需要更新，false不需要更新
	 */
	public boolean onKeyReleased(KeyEvent e){
		return View.getModelInputListener().onKeyReleased(e);
	}
	/**
	 * 更新操作面板树
	 */
	public void updateOperateTree(){
		mOperatePanel.run();
	}
	/**
	 * 刷新窗口
	 */
	public void RequireRefresh(){
		mFrameApplication.repaint();
	}
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
